package com.mustafaiev.tair.autobase.web.command;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;

import org.apache.log4j.Logger;

import com.mustafaiev.tair.autobase.dao.OrderDao;
import com.mustafaiev.tair.autobase.dao.OrderDaoImpl;
import com.mustafaiev.tair.autobase.dao.WorkerDao;
import com.mustafaiev.tair.autobase.dao.WorkerDaoImpl;
import com.mustafaiev.tair.autobase.db.DBConnector;
import com.mustafaiev.tair.autobase.order.ClientOrder;
import com.mustafaiev.tair.autobase.worker.Worker;

/**
 * 
 * The Command class implements {@link AutobaseServletCommand} and provides
 * editing the order of client
 * 
 * @author Tair Mustafaiev
 * 
 */
public class OrderEditCommandImpl implements AutobaseServletCommand {

    private static final Logger LOG = Logger.getLogger(OrderEditCommandImpl.class.getName());

    @Override
    public void processGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ClientOrder order = new ClientOrder();
        OrderDao dao = new OrderDaoImpl();
        WorkerDao workerDao = new WorkerDaoImpl();
        workerDao.setConnector(DBConnector.getInstance());
        
        if (req.getParameter("action").equals("editOrder")) {
            if (req.getParameter("orderId") != null) {
                order.setId(Integer.valueOf(req.getParameter("orderId")).intValue());
            }

            try {
                List<ClientOrder> result = dao.readOrder(order);
                List<Worker> drivers = workerDao.getDrivers();
                
                Iterator<ClientOrder> it = result.iterator();
                ClientOrder clientOrder = (ClientOrder) it.next();
                
                req.getSession().setAttribute("orderId", clientOrder.getId());
                req.getSession().setAttribute("clientName", clientOrder.getName());
                req.getSession().setAttribute("clientPhone", clientOrder.getPhone());
                req.getSession().setAttribute("orderDescription", clientOrder.getOrder());
                
                req.setAttribute("drivers", drivers);
                
                req.getSession().getServletContext()
                        .getRequestDispatcher("/admin/orderEditForm.jsp").forward(req, resp);

            } catch (SQLException e) {
                LOG.error("Cannot get order: " + e);
            }
        }
    }

    @Override
    public void processPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        ClientOrder order = new ClientOrder();
        OrderDao dao = new OrderDaoImpl();

        if (req.getParameter("action").equals("editOrder")) {
            if (req.getParameter("driverId") != null) {
                order.setDriverId(Integer.valueOf(req.getParameter("driverId")).intValue());
            }
            if (req.getParameter("orderId") != null) {
                order.setId(Integer.valueOf(req.getParameter("orderId")).intValue());
            }

            try {
                
                // set driver to order
                dao.updateOrder(order);
                LOG.info("Order with id = " + order.getId() + " added for driver with id = "
                        + order.getDriverId());
                req.getSession().setAttribute("adminMessage", "Changes was saved");
                resp.sendRedirect("/admin/orders.do");

            } catch (SQLException e) {
                LOG.error("Order update failed! " + e);
                req.getSession().setAttribute("adminError", "Failed!");
                resp.sendRedirect("/admin/orders.do");
            }
        }

    }

}
